package com.example.mylibrarybase.yumengmeng06.fragment.ViewPagerFragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.example.mylibrarybase.R;
import com.example.mylibrarybase.base.baseui.BaseFragment;
import com.example.mylibrarybase.net.netWork;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.yumengmeng04.adapter.MyListView_04_single;
import com.example.mylibrarybase.yumengmeng04.base.BaseListViewAdapter_04;
import com.example.mylibrarybase.yumengmeng04.bean.oder_04;
import com.example.mylibrarybase.yumengmeng04.view.listViewlist_04;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lzy.okgo.cache.CacheMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator 于萌萌
 * 创建日期: 11:14 . 2016年09月14日
 * 描述:未支付Fragment
 * <p/>
 * <p/>
 * 备注:
 */
public class Fragment_06 extends BaseFragment implements PullToRefreshLayout.OnRefreshListener {
    ArrayList<oder_04.ResultBean> mOders = new ArrayList<>();
    public static int fragment_status1 = 1;
    public static int fragment_status2 = 2;
    public static int fragment_status3 = 3;
    public static int fragment_status0 = 0;
    private int mUixml;
    private listViewlist_04 mFrgment_listview;
    private int mStatus;
    private MyListView_04_single mMyListView;
    private PullToRefreshLayout ptrl06;
    public CallBack mCallBack;


    public interface CallBack{
        void onRefresh(int mStatus);
        void onLoadMore(int mStatus);

    }
    public void setCallBack(CallBack mCallBack){
        this.mCallBack =mCallBack;
    }
    @Override
    public void initListener() {

    }

    @Override
    public void setStatus(int status) {

    }

    @Override
    public View initViewFragment(LayoutInflater inflater, ViewGroup container) {
        getFragment_uiView(inflater,container);
        return getFragment_uiView(inflater,container);
    }

    private View getFragment_uiView(LayoutInflater inflater, ViewGroup container) {

        return inflater.inflate(mUixml,container,false);
    }

    @Override
    public void initDataFragment(View rootView) {
       mFrgment_listview = (listViewlist_04) rootView.findViewById(R.id.frgment_listview);




       ptrl06 = ((PullToRefreshLayout) rootView.findViewById(R.id.refresh_view_fragment));
        ptrl06.setOnRefreshListener(this);


        initData(mStatus);
    }

    private void initData(int status) {
        String url = getUrl(status);



        netWork.goPost(getContext(), url, null, "1", CacheMode.NO_CACHE, new netWork.CallBack() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                mOders.clear();
                oder_04 data = jsonObject.getObject("data", oder_04.class);
                List<oder_04.ResultBean> result = data.result;
                mOders.addAll(result);
                initAdapter();
            }



            @Override
            public void onError(String error) {
                LogUtils.i("访问网络失败"+error);
            }
        });


    }

    private void initAdapter() {
      mMyListView = new MyListView_04_single(getContext(), mStatus,mOders, new BaseListViewAdapter_04.CallBack() {
            @Override
            public void loadMore(View view, int position, int type) {
               // view.setPadding(0, -200, 0, 0);
                view.setVisibility(View.GONE);
            }
        });
//      mMyListView = new MyListView_04_single(getContext(), mStatus, mUixml, R.layout.item_listview_more_04, mOders, new BaseListViewAdapter_04.CallBack() {
//            @Override
//            public void loadMore(View view, int position, int type) {
//               // view.setPadding(0, -200, 0, 0);
//                view.setVisibility(View.GONE);
//            }
//        });
        mFrgment_listview.setAdapter(mMyListView);
    }

    @Override
    public void getBundle() {
        Bundle arguments =
                getArguments();
        if (null==arguments){
            return;
        }
        mUixml = arguments.getInt("uixml");
        mStatus = arguments.getInt("status");

    }

    @Override
    public int getStatus() {
        return mStatus;
    }

    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mCallBack.onRefresh(mStatus);
        // 下拉刷新操作
        initData(mStatus,pullToRefreshLayout);

//
//        new Handler()
//        {
//            @Override
//            public void handleMessage(Message msg)
//            {
//                // 千万别忘了告诉控件刷新完毕了哦！
//                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
//
//            }
//        }.sendEmptyMessageDelayed(0, 5000);
    }

    private void initData(final int status, final PullToRefreshLayout pullToRefreshLayout) {
        String url = getUrl(status);



        netWork.goPost(getContext(), url, null, "1", CacheMode.NO_CACHE, new netWork.CallBack() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                mOders.clear();
                oder_04 data = jsonObject.getObject("data", oder_04.class);
                List<oder_04.ResultBean> result = data.result;
                mOders.addAll(result);
                mMyListView.setData(mOders);
                LogUtils.i("测试_下拉刷新网络数据获取成功"+status+"目前适配器集合长度"+mMyListView.getCount()+"内容是"+result);
            }



            @Override
            public void onError(String error) {
                LogUtils.i("访问网络失败"+error);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
            }
        });

    }

    private String getUrl(int status) {
        String url = netWork.testUrl;
        if (status ==fragment_status0){
            url = netWork.testUrl0;
        }else if (status ==fragment_status1){
            url = netWork.testUrl1;
        }else if (status ==fragment_status2){
            url = netWork.testUrl2;
        }else if (status ==fragment_status3){
            url = netWork.testUrl3;
        }
        return url;
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
        mCallBack.onLoadMore(mStatus);

        // 下拉刷新操作
        new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // 千万别忘了告诉控件刷新完毕了哦！
                initDataLoadMore(mStatus, pullToRefreshLayout);
            }
        }.sendEmptyMessageDelayed(0, 2000);
    }

    private void initDataLoadMore(final int status, final PullToRefreshLayout pullToRefreshLayout) {
        String url = getUrl(status);



        netWork.goPost(getContext(), url, null, "1", CacheMode.NO_CACHE, new netWork.CallBack() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                oder_04 data = jsonObject.getObject("data", oder_04.class);
                List<oder_04.ResultBean> result = data.result;
                mMyListView.addData(result);
                LogUtils.i("测试_上拉加载更多网络数据获取成功"+status+"目前适配器集合长度"+mMyListView.getCount()+"内容是"+result);

            }



            @Override
            public void onError(String error) {
                LogUtils.i("访问网络失败"+error);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
            }
        });

    }
}


